package cn.eeepay.framework.service.impl.tradeWarning;

import cn.eeepay.framework.dao.tradeWarning.TradeWarningDao;
import cn.eeepay.framework.model.UserLoginInfo;
import cn.eeepay.framework.model.tradeWarning.TradeWarningInfo;
import cn.eeepay.framework.service.tradeWarning.TradeWarningService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("tradeWarningService")
public class TradeWarningServiceImpl implements TradeWarningService {

    @Resource
    private TradeWarningDao tradeWarningDao;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void getTradeWarningInfo(String warningCode,Map<String, Object> msg) {
        if(StringUtils.isNotBlank(warningCode)){
            if("TRADE_ERROR".equals(warningCode)){
                TradeWarningInfo info=tradeWarningDao.getTradeWarningInfo("TRADE_ERROR");
                if(info!=null&&StringUtils.isNotBlank(info.getRulesValues())){
                    info.setTradeWarningList(JSON.parseArray(info.getRulesValues(), Map.class));
                }
                TradeWarningInfo infoB=tradeWarningDao.getTradeWarningInfo("TRADE_EXCEPTION");
                if(infoB!=null&&StringUtils.isNotBlank(infoB.getRulesValues())){
                    infoB.setTradeWarningMap((Map<String,Object>)JSON.parse(infoB.getRulesValues()));
                }
                msg.put("info",info);
                msg.put("infoB",infoB);
                msg.put("status",true);
            }else if("ZJX_ERROR".equals(warningCode)){
                TradeWarningInfo info=tradeWarningDao.getTradeWarningInfo(warningCode);
                if(info!=null&&StringUtils.isNotBlank(info.getRulesValues())){
                    info.setTradeWarningMap((Map<String,Object>)JSON.parse(info.getRulesValues()));
                }
                msg.put("info",info);
                msg.put("status",true);
            }
        }
    }

    @Override
    public int saveTradeWarningInfo(String warningCode,String param, Map<String, Object> msg) {
        //操作人
        final UserLoginInfo principal = (UserLoginInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Map<String,Object> map=(Map<String,Object>)JSON.parse(param);
        int num=0;
        if("TRADE_ERROR".equals(warningCode)){
            TradeWarningInfo info=JSON.parseObject(map.get("info").toString(),TradeWarningInfo.class);
            if(info!=null){
                info.setRulesValues(JSON.toJSONString(info.getTradeWarningList()));
                info.setUpdatePerson(principal.getUsername());
                num=tradeWarningDao.saveTradeWarningInfo(info);
                //更新后涮新rediskey
                if(redisTemplate.hasKey("WANING_CONFIG_TRADE_ERROR")){
                    redisTemplate.delete("WANING_CONFIG_TRADE_ERROR");
                }
            }

            TradeWarningInfo infoB=JSON.parseObject(map.get("infoB").toString(),TradeWarningInfo.class);
            if(info!=null){
                infoB.setRulesValues(JSON.toJSONString(infoB.getTradeWarningMap()));
                infoB.setUpdatePerson(principal.getUsername());
                tradeWarningDao.saveTradeWarningInfo(infoB);
                //更新后涮新rediskey
                if(redisTemplate.hasKey("WANING_CONFIG_TRADE_EXCEPTION")){
                    redisTemplate.delete("WANING_CONFIG_TRADE_EXCEPTION");
                }
            }
        }else if("ZJX_ERROR".equals(warningCode)){
            TradeWarningInfo info=JSON.parseObject(map.get("info").toString(),TradeWarningInfo.class);
            if(info!=null){
                info.setRulesValues(JSON.toJSONString(info.getTradeWarningMap()));
                info.setUpdatePerson(principal.getUsername());
                num=tradeWarningDao.saveTradeWarningInfo(info);
                //更新后涮新rediskey
                if(redisTemplate.hasKey("WANING_CONFIG_ZJX_ERROR")){
                    redisTemplate.delete("WANING_CONFIG_ZJX_ERROR");
                }
            }
        }
        if(num>0){
            msg.put("msg","保存成功");
            msg.put("status",true);
        }else{
            msg.put("msg","保存失败");
            msg.put("status",false);
        }
        return 0;
    }
}
